package java3.io;

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static final int white = 0;
    public static final int black = 1;
    public static final int vacio = -1;
    private boolean[] enroques;
    private int[][] tablero;
    private boolean isWhite;
    private UtilLoser util;

    private void inicializarVariables(boolean[] enroques, int[][] tablero, boolean isWhite) {
        this.enroques = enroques;
        this.tablero = tablero;
        this.isWhite = isWhite;
        this.util = new UtilLoser();
    }

    public static void main(String[] args) {
        Main m = new Main();
        m.iniciarJuego();
//        m.probarjugada();
    }

    private void iniciarJuego() {
        Posiciones pos = new Posiciones();
        Loserchess loser = null;
        String command;

        Scanner sca = new Scanner(System.in);
        String in;
        int[] j = new int[4];
        int[] out;
        int num = 1;

        System.out.println("you welcome, please insert command...");
        bucle_principal:
        while (true) {
            //esperando comando
            command = sca.nextLine();

            switch (command) {
                case "quit":
                    break bucle_principal;
                case "new white":
                case "new black":
                    switch (command) {
                        case "new white":
                            this.inicializarVariables(new boolean[]{true, true, true, true}, pos.inicio(), true);
                            loser = new Loserchess(black);
                            System.out.println("human play!!");
                            num = 1;
                            break;
                        case "new black":
                            this.inicializarVariables(new boolean[]{true, true, true, true}, pos.inicio(), false);
                            loser = new Loserchess(white);
                            System.out.println("machine play!!");
                            num = 1;

                            out = loser.jugar(tablero, j[1], j[2], new boolean[]{enroques[0], enroques[1], enroques[2], enroques[3]});

                            util.ingresarJugadayVerificarEnroques(tablero, enroques, out, !isWhite);
                            System.out.print(num + ") " + util.imprimirJugada(out) + "\t");
                            break;
                    }
                    bucle_secundario:
                    while (true) {
                        //leer jugada
                        in = sca.nextLine();
                        switch (in) {
                            case "quit":
                                System.out.println("please insert command...");
                                break bucle_principal;
                            case "finish":
                                System.out.println("game abort...");
                                break bucle_secundario;
                            default:
                                if (!in.isEmpty() && (in.length() == 4 || (in.length() == 5
                                        && (in.endsWith("Q") || in.endsWith("R") || in.endsWith("B") || in.endsWith("N"))))) {
                                    j = util.obtenerJugadadeString(in, tablero);
                                    if (util.ingresarJugadayVerificarEnroques(tablero, enroques, j, isWhite)) {
                                        System.out.println("you lose, machine win!!!");
                                        break bucle_secundario;
                                    }
                                    if (!isWhite) {
                                        System.out.print(in + "\n");
                                        System.out.println("\n" + util.imprimirTablero(tablero, !isWhite));
                                        System.out.println(Arrays.toString(enroques));
                                    }
                                    //juega máquina
                                    out = loser.jugar(tablero, j[1], j[2], new boolean[]{enroques[0], enroques[1], enroques[2], enroques[3]});
                                    if (out == null) {
                                        System.out.println("no exist move!!!");
                                        break bucle_secundario;
                                    }
                                    if (util.ingresarJugadayVerificarEnroques(tablero, enroques, out, !isWhite)) {
                                        System.out.println("machine lose, you win!!!");
                                        break bucle_secundario;
                                    }
                                    if (isWhite) {
                                        System.out.print(num + ") " + in + "\t" + util.imprimirJugada(out) + "\n");
                                        System.out.println("\n" + util.imprimirTablero(tablero, !isWhite));
                                        System.out.println(Arrays.toString(enroques));
                                    } else {
                                        System.out.print((num = num + 1) + ") " + util.imprimirJugada(out) + "\t");
                                    }
                                    num++;
                                } else {
                                    System.out.println("jugada inválida");
                                }
                                break;
                        }
                    }
                    break;
                default:
                    System.out.println("comando inválido");
            }
        }
    }

    private void probarjugada() {
        Posiciones pos = new Posiciones();
        Loserchess loser;

        Scanner sca = new Scanner(System.in);
        String in;
        int[] j = new int[4];
        int[] out;
        int num;

        this.inicializarVariables(new boolean[]{false, false, true, true}, pos.temp(), true);
        loser = new Loserchess(black);
        System.out.println("human play!!");
        num = 1;

//        this.inicializarVariables(new boolean[]{true, false, true, true}, pos.temp(), false);
//        loser = new Loserchess(white);
//        System.out.println("machine play!!");
//        num = 1;

        System.out.println("\n" + util.imprimirTablero(tablero, !isWhite));
        System.out.println(Arrays.toString(enroques));

        out = loser.jugar(tablero, j[1], j[2], new boolean[]{enroques[0], enroques[1], enroques[2], enroques[3]});

        util.ingresarJugadayVerificarEnroques(tablero, enroques, out, !isWhite);
        System.out.print(num + ") " + util.imprimirJugada(out) + "\t");



        while (true) {
            //leer jugada
            in = sca.nextLine();
            switch (in) {
                case "quit":
                    System.out.println("please insert command...");
                    break;
                case "finish":
                    System.out.println("game abort...");
                    break;
                default:
                    if (!in.isEmpty() && (in.length() == 4 || (in.length() == 5
                            && (in.endsWith("Q") || in.endsWith("R") || in.endsWith("B") || in.endsWith("N"))))) {
                        j = util.obtenerJugadadeString(in, tablero);
                        if (util.ingresarJugadayVerificarEnroques(tablero, enroques, j, isWhite)) {
                            System.out.println("you lose, machine win!!!");
                            break;
                        }
                        if (!isWhite) {
                            System.out.print(in + "\n");
                            System.out.println("\n" + util.imprimirTablero(tablero, !isWhite));
                            System.out.println(Arrays.toString(enroques));
                        }
                        //juega máquina
                        out = loser.jugar(tablero, j[1], j[2], new boolean[]{enroques[0], enroques[1], enroques[2], enroques[3]});
                        if (util.ingresarJugadayVerificarEnroques(tablero, enroques, out, !isWhite)) {
                            System.out.println("machine lose, you win!!!");
                            break;
                        }
                        if (isWhite) {
                            System.out.print(num + ") " + in + "\t" + util.imprimirJugada(out) + "\n");
                            System.out.println("\n" + util.imprimirTablero(tablero, !isWhite));
                            System.out.println(Arrays.toString(enroques));
                        } else {
                            System.out.print((num = num + 1) + ") " + util.imprimirJugada(out) + "\t");
                        }
                        num++;
                    } else {
                        System.out.println("jugada inválida");
                    }
                    break;
            }
        }
    }
}
